﻿using EPE.BLL;
using EPE.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace EPE.WPF
{
    /// <summary>
    /// UCShopsPriceSystem.xaml 的交互逻辑
    /// </summary>
    public partial class UCShopsPriceSystemList : UserControl
    {
        public UCShopsPriceSystemList()
        {
            InitializeComponent();
        }

        CLoginUser userinfo = new CLoginUser();
        public UCShopsPriceSystemList(CLoginUser pargUserinfo)
        {
            InitializeComponent();
            userinfo = pargUserinfo;

            BindData();
            
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            BindData();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            ShopsPriceSystemAddForm frm = new ShopsPriceSystemAddForm(userinfo);
            frm.ShowDialog();
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnDel_Click(object sender, RoutedEventArgs e)
        {

        }

        private ObservableCollection<CShopsPriceParaTree> ShopsPriceParaTree;
        private ObservableCollection<CShopsPriceParaTree> DataSource;
        private void BindData()
        {
            ShopsPricePara bll = new ShopsPricePara();
            ShopsPriceParaTree = bll.GetAllList(string.Empty,userinfo.CompanyId.ToString());

            DataSource = new ObservableCollection<CShopsPriceParaTree>();
            foreach (var p in ShopsPriceParaTree)
            {
                p.PropertyChanged += p_PropertyChanged;
                DataSource.Add(p);
            }
            TreeGrid.ItemsSource = DataSource;
        }

        void p_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            var selectedObj = sender as CShopsPriceParaTree;
            if (selectedObj == null)
                return;
            if (selectedObj.IsChecked)
            {
                int next = DataSource.IndexOf(selectedObj) + 1;
                for (int i = 0; i < selectedObj.Subordinates.Count; i++)
                {
                    var p = selectedObj.Subordinates[i];
                    if (DataSource.FirstOrDefault(t => t.paraid == p.paraid) != null)
                        return;
                    p.PropertyChanged += p_PropertyChanged;
                    p.IsChecked = false;
                    DataSource.Insert(next++, p);
                }
            }
            else if (!selectedObj.IsChecked)
            {
                for (int i = 0; i < selectedObj.Subordinates.Count; i++)
                {
                    var p = selectedObj.Subordinates[i];
                    RemoveNode(p);
                }
            }
        }

        private void RemoveNode(CShopsPriceParaTree sp)
        {
            for (int i = 0; i < sp.Subordinates.Count; i++)
            {
                var p = sp.Subordinates[i];
                RemoveNode(p);
            }
            for (int i = 0; i < DataSource.Count; i++)
            {
                var p = DataSource[i];
                if (p.paraid == sp.paraid)
                    DataSource.Remove(p);
            }
        }

    }
}
